package main

import (
	"fmt"
	"regexp"
	"strings"
	"time"
)

func main() {
	str, err := timeFormat("2021-12-18 11:47:18")
	if err != nil {
		fmt.Printf("timeFormat err : %s \n", err)
	}
	fmt.Println("final: " + str)

	timeObj, _ := time.ParseInLocation("2006-01-02 15:04:05", "2021-12-18 11:47:18", time.Local)
	sysTime := timeObj.Unix()
	nowTime := time.Now().Unix()
	fmt.Println("sys_time:", sysTime)
	fmt.Println("now_time:", nowTime)
	fmt.Println("now_time - sys_time = ", nowTime-sysTime)
	//arr := []int{1, 9, 10, 30, 2, 5, 45, 8, 63, 234, 12}
	//fmt.Println(QuickSort(arr))
	//fmt.Println(HeapSort(arr))
	//fmt.Println(GetMax(arr))
	//fmt.Println(BubbleSort(arr))
}

func timeFormat(timeStr string) (string, error) {
	reg := regexp.MustCompile(`\/`)
	str := string(reg.ReplaceAll([]byte(timeStr), []byte("-")))
	reg = regexp.MustCompile(`T|g|\.[\d]{3}Z`)
	str = strings.Trim(string(reg.ReplaceAll([]byte(str), []byte(" "))), " ")
	fmt.Println(str)
	now, err := time.ParseInLocation("2006-01-02 15:04:05", str, time.Local)
	if err != nil {
		now, err = time.Parse("2006-01-02", str)
		if err != nil {
			fmt.Printf("日期数据非法 err:%s \n", err)
			return "", err
		}
	}
	return now.Format("2006-01-02 15:04:05"), nil
}

//快读排序算法
func QuickSort(arr []int) []int {
	if len(arr) <= 1 {
		return arr
	}
	splitdata := arr[0]          //第一个数据
	low := make([]int, 0, 0)     //比我小的数据
	hight := make([]int, 0, 0)   //比我大的数据
	mid := make([]int, 0, 0)     //与我一样大的数据
	mid = append(mid, splitdata) //加入一个
	for i := 1; i < len(arr); i++ {
		if arr[i] < splitdata {
			low = append(low, arr[i])
		} else if arr[i] > splitdata {
			hight = append(hight, arr[i])
		} else {
			mid = append(mid, arr[i])
		}
	}
	low, hight = QuickSort(low), QuickSort(hight)
	myarr := append(append(low, mid...), hight...)
	return myarr
}

//堆排序
func HeapSortMax(arr []int, length int) []int {
	// length := len(arr)
	if length <= 1 {
		return arr
	}
	depth := length/2 - 1 //二叉树深度
	for i := depth; i >= 0; i-- {
		topmax := i //假定最大的位置就在i的位置
		leftchild := 2*i + 1
		rightchild := 2*i + 2
		if leftchild <= length-1 && arr[leftchild] > arr[topmax] { //防止越过界限
			topmax = leftchild
		}
		if rightchild <= length-1 && arr[rightchild] > arr[topmax] { //防止越过界限
			topmax = rightchild
		}
		if topmax != i {
			arr[i], arr[topmax] = arr[topmax], arr[i]
		}
	}
	return arr
}
func HeapSort(arr []int) []int {
	length := len(arr)
	for i := 0; i < length; i++ {
		lastlen := length - i
		HeapSortMax(arr, lastlen)
		if i < length {
			arr[0], arr[lastlen-1] = arr[lastlen-1], arr[0]
		}
	}
	return arr
}

//冒泡排序获取最大值
func GetMax(arr []int) int {
	for i := 1; i < len(arr); i++ {
		if arr[i-1] > arr[i] {
			arr[i-1], arr[i] = arr[i], arr[i-1]
		}
	}
	return arr[len(arr)-1]
}

//冒泡排序
func BubbleSort(arr []int) []int {
	for i := 0; i < len(arr); i++ {
		for j := i + 1; j < len(arr); j++ {
			if arr[i] > arr[j] {
				arr[j], arr[i] = arr[i], arr[j]
			}
		}
	}
	return arr
}
